home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / files / standard file / customputappend / customputappend.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  6.6 KB  |  185 lines

  1. /*
  2.     File:        CustomPutAppend.c
  3.  
  4.     Contains:    This demonstrates a CustomPutDialog with an Append button that
  5.                 does not ask the user if he/she wants to replace the existing file.
  6.                  the sample code in InsideMac:Files, and the Human Interface Guidelines                It also has Baloon Help strings for the Append button
  7.  
  8.     Written by: David Hayward    
  9.  
  10.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 7/1/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  22.                 5/12/95                        updated project for Metrowerks
  23.                 2/3/93                        first draft
  24.                 
  25.  
  26. */
  27.  
  28.  
  29. #include <QuickDraw.h>
  30. #include <Memory.h>
  31. #include <Fonts.h>
  32. #include <StandardFile.h>
  33. #include <Files.h>
  34. #include <Dialogs.h>
  35. #include <TextUtils.h>
  36.  
  37. #include "InitMac.h"
  38.  
  39.  
  40. /**\
  41. |**| ==============================================================================
  42. |**| DEFINES
  43. |**| ==============================================================================
  44. \**/
  45. #define    ReplyDialogID            128
  46. #define    ReplyDialogQuitItem        1
  47. #define    ReplyDialogAgainItem    2
  48.  
  49. #define    CustomPutAppendDialogID    129
  50. #define    sfItemAppendButton        13
  51.  
  52.  
  53. /**\
  54. |**| ==============================================================================
  55. |**| FUNCTION PROTOTYPES
  56. |**| ==============================================================================
  57. \**/
  58. short            MyAlert        ( StandardFileReply *reply ) ;
  59. pascal short    MyDlgHook    ( short item, DialogPtr theDialog, void *myDataPtr ) ;
  60.  
  61.                             
  62. /**\
  63. |**| ==============================================================================
  64. |**| FUNCTIONS
  65. |**| ==============================================================================
  66. \**/
  67.  
  68. /*------------------------------------------------------------------------------*\
  69.     MyDlgHook()
  70.  *------------------------------------------------------------------------------*
  71.         the dialog hook procedure responsible for activating and  
  72.         dimming the Append control and supressing the "Replace Existing?" 
  73.         dialog if the Append button is hit
  74. \*------------------------------------------------------------------------------*/
  75. pascal short MyDlgHook ( short item, DialogPtr theDialog, void *myDataPtr )
  76. {
  77.     static short        lastItem = 0;            /* remember lastItem so that we dont update    */
  78.                                                 /*  Append button on consecutive NullEvents    */
  79.     static short        which;                    /* remember if Save or Append was hit so we    */
  80.                                                 /*  can dismiss the "Replace" dialog if needed */
  81.     short                returnItem = item;        /* default returnItem is item */
  82.     long                refCon;
  83.     short                iType;
  84.     Handle                iHandle;
  85.     Rect                iRect;
  86.     FInfo                fndrInfo;
  87.     OSErr                err;
  88.     StandardFileReply    *sfr;
  89.  
  90.     refCon = GetWRefCon((WindowPtr)theDialog);    /* get the refCon of the current dialog */
  91.  
  92.     if (refCon == sfReplaceDialogRefCon)            /* if its the "Replace Existing?" dialog */
  93.         if (item == sfHookFirstCall)                /* and the dialog is just about to appear */
  94.             if (which == sfItemAppendButton)        /* and user last hit the append button */
  95.                 returnItem = 1;                        /* then hit the Replace button automatically */
  96.  
  97.     if (refCon == sfMainDialogRefCon)                /* if its the "Save As…" dialog */
  98.     {
  99.  
  100.         sfr = (StandardFileReply*)myDataPtr;        /* coerce myDataPtr to StandardFileReply */
  101.             
  102.         /* update Append button state... */
  103.         if ( (item == sfHookFirstCall) ||            /* if the dialog is just about to appear */
  104.              ( (item == sfHookNullEvent) &&            /* or if we just got a NullEvent */
  105.                (lastItem != sfHookNullEvent)))
  106.         {
  107.             GetDialogItem(theDialog,                        /* get the handle for the Append button */
  108.                      sfItemAppendButton,
  109.                      &iType, &iHandle, &iRect);
  110.             
  111.             /* GetFInfo with the current filespec to see it it exists */
  112.             err = FSpGetFInfo(&(sfr->sfFile), &fndrInfo);
  113.             
  114.             if ((err)    ||                            /* if file doesn't exist or */
  115.                 (fndrInfo.fdType != 'TEXT'))        /* isn't 'TEXT', then dim Append */
  116.                 HiliteControl((ControlHandle)iHandle, 255);
  117.             else                                    /* otherwise, activate Append Button */
  118.                 HiliteControl((ControlHandle)iHandle, 0);
  119.         }
  120.  
  121.  
  122.         if (item==sfItemOpenButton)                    /* if user hit Save button */
  123.             which = sfItemOpenButton;                /* then remember this is what was hit */
  124.  
  125.         if (item==sfItemAppendButton)                /* if user hit Append button */
  126.         {
  127.             which = sfItemAppendButton;                /* then remember this is what was hit */
  128.             sfr->sfReplacing |= 2;                    /* set 2nd bit so we we know it was append */
  129.             returnItem = sfItemOpenButton;            /* but perform a Save instead */
  130.         }
  131.     }
  132.     
  133.     lastItem = item;                                /* save lastItem */
  134.     return returnItem;
  135. }
  136.  
  137.  
  138. /*------------------------------------------------------------------------------*\
  139.     MyAlert()
  140.  *------------------------------------------------------------------------------*
  141.         output vital stats of the StandardFileReply to an Alert box
  142. \*------------------------------------------------------------------------------*/
  143. short MyAlert ( StandardFileReply *reply )
  144. {
  145.     Str255    goodStr, replStr;
  146.  
  147.     NumToString( (long)reply->sfGood, goodStr); 
  148.     NumToString( (long)reply->sfReplacing, replStr); 
  149.     ParamText(goodStr, replStr, reply->sfFile.name, nil);
  150.     return NoteAlert( ReplyDialogID, nil );
  151. }
  152.  
  153.  
  154. /*------------------------------------------------------------------------------*\
  155.     main()
  156.  *------------------------------------------------------------------------------*
  157.         initialize mamagers and the keep doing 
  158.         CustomPutFile until the user has had enough
  159. \*------------------------------------------------------------------------------*/
  160. void main ( void )
  161. {
  162.     StandardFileReply    reply;
  163.     Point                where = {-1,-1};            /* center dialog on main screen */
  164.     
  165.     InitToolBox(1) ;
  166.     
  167.     do
  168.     {
  169.         CustomPutFile(    "\pSave file as:",            /* the prompt string */
  170.                         "\pUntitled",                /* the default filename */
  171.                         &reply,                        /* the StandardFileReply structure */
  172.                         CustomPutAppendDialogID,    /* the dialog's ID */
  173.                         where,                        /* position of dialog on screen */
  174.                         NewDlgHookYDProc(MyDlgHook),    /* the Dialog Hook procedure */
  175.                         nil,                        /* no modal dialog filterProc */
  176.                         nil,                        /* no activeListPtr */
  177.                         nil,                        /* no activateProc */
  178.                         &reply                        /* yourDataPtr: pass reply in so that */
  179.                       );                            /* MyDlgHook() can access current FSSpec */
  180.     }                                                            
  181.     while (MyAlert(&reply)==ReplyDialogAgainItem);    /* repeat until user has had enough */
  182. }
  183.  
  184.  
  185.